package com.ruoyi.stuclient.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.stuclient.domain.EwcHomeworkSubmission;
import com.ruoyi.stuclient.domain.EwcHomeworkSubmissionMaterials;
import com.ruoyi.stuclient.domain.dto.HomeworkSubmitDto;
import com.ruoyi.stuclient.mapper.EwcHomeworkSubmissionMapper;
import com.ruoyi.stuclient.service.IEwcHomeworkSubmissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * 课后作业提交Service业务层处理
 * 
 * @author lcj
 * @date 2025-02-28
 */
@Service
public class EwcHomeworkSubmissionServiceImpl extends ServiceImpl<EwcHomeworkSubmissionMapper, EwcHomeworkSubmission> implements IEwcHomeworkSubmissionService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String homeworkSubmission(HomeworkSubmitDto homeworkSubmitDto) {
        ewcHomeworkSubmission.setHomeworkSubmissionId(UUID.randomUUID().toString().replace("-", ""));
        ewcHomeworkSubmission.setHomeworkId(homeworkSubmitDto.getHomeworkId());
        ewcHomeworkSubmission.setStudentId(ThirdSessionHolder.getThirdSession().getId());
        Date currentDate = new Date();
        ewcHomeworkSubmission.setSubmissionDate(currentDate);
        boolean result = save(ewcHomeworkSubmission);
        JSONObject resultJson = new JSONObject();
        if(result){
            return ewcHomeworkSubmission.getHomeworkSubmissionId();
        }
        return "";
    }

    @Override
    public EwcHomeworkSubmission getSubmissionHomework(String homeworkId) {
        QueryWrapper<EwcHomeworkSubmission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("homework_id", homeworkId);
        queryWrapper.eq("student_id", ThirdSessionHolder.getThirdSession().getId());
        return getOne(queryWrapper);
    }

    @Override
    public Boolean deleteSubmissionHomework(String homeworkId) {
        QueryWrapper<EwcHomeworkSubmission> queryWrapper = new QueryWrapper<>();
        // 根据实体属性设置删除条件
        queryWrapper.eq("homework_id", homeworkId);
        queryWrapper.eq("student_id", ThirdSessionHolder.getThirdSession().getId());
        // 执行删除操作
        int rows = this.baseMapper.delete(queryWrapper);
        return rows > 0;
    }
}
